package com.timer;

import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * https://developer.ibm.com/zh/articles/j-lo-taskschedule/
 */
public class ScheduledExecutor implements Runnable{
    int count ;
    private String jobName = "";
    ScheduledExecutorService scheduledExecutorService;
    public ScheduledExecutor(String jobName) {
        super();
        this.count =1;
        this.jobName = jobName;
        this.scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
    }

    @Override
    public void run() {
        if(count>=5){
            System.out.println(String.format("今天晚上:%s,%s次，明天再来",jobName,count));
           scheduledExecutorService.shutdown();

        }
        System.out.println(String.format("执行任务:%s,开始时间:%s,操了%s次",jobName,new Date(),count));
        count++;

    }

    public static void main(String[] args) {
        Long delay = (long)1000;
        Long period = (long)1000;
        TimeUnit milliseconds = TimeUnit.MILLISECONDS;
        ScheduledExecutor job = new ScheduledExecutor("操妹妹");
        job.scheduledExecutorService.scheduleAtFixedRate(job,delay,period,milliseconds);
        ScheduledExecutor job2 = new ScheduledExecutor("操哥哥");
        job2.scheduledExecutorService.scheduleAtFixedRate(job2,delay,period,milliseconds);
    }

}
